package org.tobaccomain.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.tobaccomain.mapper.DeskMapper;
import org.tobaccomain.pojo.dto.PageExtra;
import org.tobaccomain.pojo.dto.TobaccoInfo;
import org.tobaccomain.pojo.entity.Result;
import org.tobaccomain.pojo.entity.Rfid;
import org.tobaccomain.pojo.entity.TobaccoBasic;
import org.tobaccomain.pojo.vo.PageResult;
import org.tobaccomain.service.ExtraService;
import org.tobaccomain.utils.RfidUtils;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
public class ExtraController {
    @Autowired
    private ExtraService extraService;
    @Autowired
    private DeskMapper deskMapper;

    //备料信息的添加
    @PostMapping("/extra")
    public Result addExtra(@RequestBody TobaccoBasic tobaccoBasic){
        extraService.addExtra(tobaccoBasic);
        return Result.success("信息添加成功");
    }

    //备料信息的删除
    @DeleteMapping("/extra")
    public  Result delExtra(TobaccoBasic tobaccoBasic){
        extraService.delExtra(tobaccoBasic);
        return Result.success("信息删除成功");
    }

    //备料备注信息修改
    @PutMapping("/extra/info")
    public Result modifyExtraInfo(@RequestBody TobaccoInfo extraInfo){
        extraService.modifyExtraInfo(extraInfo);
        return Result.success("备注修改成功");
    }

    //备料信息修改
    @PutMapping("/extra")
    public  Result modifyExtra(@RequestBody TobaccoBasic tobaccoBasic){
        extraService.modifyExtra(tobaccoBasic);
        return Result.success("基本信息编辑成功");
    }

    //备料的分页查询
    @GetMapping("/extra/page")
    public Result getExtraList( PageExtra pageExtra){
        PageResult pageResult = extraService.getExtraList(pageExtra);
        return Result.success(pageResult);
    }

    //根据id查询备料信息
    @GetMapping("/extra")
    public Result getExtraById(Integer id){
        TobaccoBasic tobaccoBasic = extraService.getExtraById(id);
        return Result.success(tobaccoBasic);
    }

    //根据rfid查找备料位置
    @PostMapping("/extra/show")
    public Result showExtraPlace(@RequestBody List<Long> rfidList){
        if (rfidList.isEmpty()){
            return Result.success("没有数据");
        }
        RfidUtils.search(rfidList);
        //从数据库中查找rfid的信息（用作前端位置的显示）
        List<Rfid> list = deskMapper.selectRfidInfo(rfidList);
        return Result.success(list);
    }

    //备料信息导入
    @PostMapping("/extra/upload")
    public Result uploadExtra(MultipartFile file) throws IOException {
        extraService.uploadExtra(file);
        return Result.success("备料信息上传成功");
    }
}
